package com.xujianlong.day03;

import com.xujianlong.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink07_TransForm_Max_MaxBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> dataStreamSource = env.socketTextStream("hadoop102", 9999);
        SingleOutputStreamOperator<WaterSensor> map = dataStreamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String s) throws Exception {
                String[] fields = s.split(",");
                return new WaterSensor(fields[0], Long.parseLong(fields[1]), Integer.parseInt(fields[2]));
            }
        });
        SingleOutputStreamOperator<WaterSensor> maxby = map.keyBy(WaterSensor::getId).maxBy("vc");

        SingleOutputStreamOperator<WaterSensor> maxbyTrue = map.keyBy(WaterSensor::getId).maxBy("vc", true);

        SingleOutputStreamOperator<WaterSensor> maxbyfalse = map.keyBy(WaterSensor::getId).maxBy("vc", false);

        maxby.print("maxby");

//        maxbyTrue.print("maxbyTrue");
//
//        maxbyfalse.print("maxbyfalse");

        env.execute();
    }
}
